﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gallio.Framework;
using MbUnit.Framework;
using MbUnit.Framework.ContractVerifiers;
using System.IO;

namespace PTP.Core
{
  [TestFixture]
  public class DataUtilTest
  {
    [Test]
    [Row(new byte[] { 0x00, 0x00 }, 0)]
    [Row(new byte[] { 0x01, 0x00 }, 1)]
    [Row(new byte[] { 0x02, 0x00 }, 2)]
    [Row(new byte[] { 0xff, 0x00 }, 255)]
    [Row(new byte[] { 0x00, 0x01 }, 256)]
    [Row(new byte[] { 0x01, 0x01 }, 257)]
    public void ReadUINT16(byte[] data, uint expectedResult)
    {
      using (MemoryStream stream = new MemoryStream(data))
      {
        Assert.AreEqual<uint>(expectedResult, DataUtil.ReadUINT16(stream));
        Assert.AreEqual<long>(stream.Length, stream.Position, "Must read all bytes");
      }
    }

    [Test]
    [Row(new byte[] { 0x00 }, "")]
    [Row(new byte[] { 0x05, 0x54, 0x00, 0x65, 0x00, 0x73, 0x00, 0x74, 0x00, 0x00, 0x00 }, "Test")]
    public void ReadString(byte[] data, string expectedResult)
    {
      using (MemoryStream stream = new MemoryStream(data))
      {
        Assert.AreEqual<string>(expectedResult, DataUtil.ReadString(stream));
        Assert.AreEqual<long>(stream.Length, stream.Position, "Must read all bytes");
      }
    }
  }
}
